//axios封装
import axios from "axios";
import { getToken, removeToken } from '@/utils';
import router from "@/router";

const requst = axios.create(
  {
    baseURL: 'http://geek.itheima.net/v1_0',
    timeout: 5000
  }
)
//请求拦截器
requst.interceptors.request.use((config) => {
  const token = getToken()
  token && (config.headers.Authorization = `Bearer ${token}`)
  return config
}, (error) => {
  return Promise.reject(error)
})

//响应拦截器
requst.interceptors.response.use((response) => {
  return response.data
}, (error) => {
  //401，Token失效
  console.dir(error)
  if (error.response.status === 401) {
    removeToken()
    router.navigate('/login')
    window.location.reload()
  }
  return Promise.reject(error)
})




export { requst }